#include "TimePoint.h"
/*
构造函数
*/


TimePoint::TimePoint(){}

TimePoint::TimePoint(time_t t){set(t);}

TimePoint::TimePoint(const char *s){set(s);}

TimePoint::TimePoint(const string &s){set(s);}
/*end for a block*/


/*
set函数
*/

void TimePoint::set(time_t t)
{
	struct tm *tp;
	tp = localtime(&t);
	
	year = tp->tm_year+1900;
	mon = tp->tm_mon+1;
	day = tp->tm_mday;
	
	hour = tp->tm_hour;
	min = tp->tm_min;
	sec = tp->tm_sec;
}

void TimePoint::set(const char *s)
{
	sscanf(s,"%d-%d-%d%d:%d:%d",
			&year,&mon,&day,
			&hour,&min,&sec);
	set(time_t(*this));
}

void TimePoint::set(const string &s)
{
	set(s.c_str());
}
/*end for a block*/


/*
类型转换函数
*/

TimePoint::operator time_t()const
{
	struct tm tmp;
	tmp.tm_year = year-1900;
	tmp.tm_mon = mon-1;
	tmp.tm_mday = day;
	tmp.tm_hour = hour;
	tmp.tm_min = min;
	tmp.tm_sec = sec;
	tmp.tm_isdst = 0;
	time_t ans = mktime(&tmp);
	return ans;
}

TimePoint::operator string()const
{
	ostringstream ans;
	ans<<year<<"-"<<mon<<"-"<<day<<" "<<
		hour<<":"<<min<<":"<<sec;
	return string(ans.str());
}
/*end for a block*/


/*
时间加减
*/

TimePoint TimePoint::operator +(time_t tlen)
{
	time_t ta,tc;
	ta = (time_t)(*this);
	tc = ta+tlen;
	TimePoint c;
	c.set(tc);
	return c;
}

TimePoint TimePoint::operator -(time_t tlen)
{
	return (*this)+(-tlen);
}

TimePoint TimePoint::operator +(TimeLen tlen)
{
	return (*this)+(time_t)tlen;
}

TimePoint TimePoint::operator -(TimeLen tlen)
{
	return (*this)-(time_t)tlen;
}

time_t TimePoint::operator -(TimePoint b)
{
	return (time_t)(*this)-(time_t)b;
}
/*end for a block*/


/*辅助函数*/
int TimePoint::get_wday()
{
	time_t t;
	t = time_t(*this);
	struct tm *tp;
	tp = localtime(&t);
	return tp->tm_wday;
}
/*end for a block*/